const moment = require('moment'); 
//model 数据层
module.exports = class testModel extends require('./Model') {
    //查找
    static findCateList(req, res) {
        return new Promise((resolve, reject) => {
            let sql = 'select * from baking_cate'
            this.query(sql)// 调用基类的query方法往里面输入sql语句
                .then((r) => {
                    resolve(r)
                })
                .catch((e) => {
                    reject(e)
                })
        })
    }

    //查找分类信息
    static findCateListByCond(req, res) {
        return new Promise((resolve, reject) => {
            let { id, name } = req.query; // 假设查询条件通过请求的 query 参数传递
    
            let sql = '';
            let params = [];
    
            if (id) {
                sql = 'SELECT * FROM baking_cate WHERE id = ?';
                params.push(id);
            } else if (name) {
                sql = 'SELECT * FROM baking_cate WHERE name LIKE ?';
                params.push(`%${name}%`);
            } else {
                sql = 'SELECT * FROM baking_cate';
            }
    
            this.query(sql, params) // 调用基类的 query 方法执行 SQL 语句
                .then((r) => {
                    resolve(r);
                })
                .catch((e) => {
                    reject(e);
                });
        });
    }

    //增加分类数据
    static addCateList(req, res) {
        return new Promise((resolve, reject) => {
            let info = req.body;
            const currentTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 当前时间

            // 设置 created_at 和 updated_at
            info.create_time = currentTime;
            info.update_time = currentTime;
            let sql = 'INSERT INTO baking_cate SET ?';
    
            this.query(sql, info)
                .then((r) => {
                    // 成功添加数据
                    res.send({ status: 1, message: 'ok', successMessage: 'Category added successfully' });
                })
                .catch((e) => {
                    // 处理错误
                    res.send({ status: 0, message: 'error', errorMessage: 'Error adding category' });
                });
        });
    }


    //修改分类信息
    //注意mysql配置是默认不识别大小写的，可以对mysql字段中的排序规则设置为 utf8mb4_bin 这样mysql中你要修改的字段就可以实现区分大小
    static async updateCateList(req, res) {
        const { id, newName } = req.body;
        const sql = 'UPDATE baking_cate SET name = ?, update_time = CURRENT_TIMESTAMP WHERE id = ?';

        try {
            const result = await this.query(sql, [newName, id]);
            res.send({ status: 1, message: 'ok', successMessage: 'Category updated successfully' });
        } catch (error) {
            res.status(500).send({ status: 0, message: 'error', errorMessage: 'Error updating category' });
        }
    }

    // 删除分类信息
    static async deleteCateList(req, res) {
        const { id } = req.body; // 假设 id 是通过 URL 参数传递的
        console.log(id)
        const sql = 'DELETE FROM baking_cate WHERE id = ?';

        try {
            const result = await this.query(sql,id);
            if (result.affectedRows === 1) {
                res.json({ status: 1, message: 'ok', successMessage: 'Category deleted successfully' });
            } else {
                res.status(404).json({ status: 0, message: 'error', errorMessage: 'Category not found' });
            }
        } catch (error) {
            res.status(500).json({ status: 0, message: 'error', errorMessage: 'Error deleting category' });
            console.log(result)
        }
    }
}